package com.dao.impl;

import com.dao.BuyerDao;
import com.entity.Buyer;
import com.entity.BuyerExample;
import com.mapper.BuyerMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BuyerDaoImpl implements BuyerDao {
    @Resource
    BuyerMapper buyerMapper;

    /**
     * 根据管理员编号查询一个管理员
     * @param buid  管理员编号
     * @return 管理员对象
     */
    public Buyer getByBuid(String buid){
        return buyerMapper.selectByPrimaryKey(buid);
    }
    public List<Buyer> getAll(){
         return null;
        //return buyerMapper.getAll();
    }
    public List<Buyer> getAllByPage(PageInfo pageInfo){
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) buyerMapper.countByExample(new BuyerExample()));
       return null;
        // return buyerMapper.getAllByPage(map);
    }

    @Override
    public List<Buyer> searchNameByPage(PageInfo pageInfo,String adname) {

        BuyerExample buyerExample = new BuyerExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        buyerExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        buyerExample.setLimit(limit);
        //createCriteria 创建一个条件查询
       // buyerExample.createCriteria().andAdnameLike(adname);
        return buyerMapper.selectByExample(buyerExample);
    }

    /**
     * 根据主键查询
     * @param buid
     * @return
     */
    public Buyer getBuyerByBuid(String buid){
        return buyerMapper.selectByPrimaryKey(buid);
    }

    /**
     * 添加一个实体
     * @param buyer
     * @return
     */
    public int addBuyer(Buyer buyer){
        return buyerMapper.insertSelective(buyer);
    }
    /**
     * 修改实体,如果有条件 需要设置example
     */
    public int updateBuyer(Buyer buyer){
        return buyerMapper.updateByPrimaryKeySelective(buyer);
    }
    public int updateBuyerByExample(Buyer buyer){
        return buyerMapper.updateByExample(buyer,null);
    }

    /**
     * 删除一个实体
     * @param buid
     * @return
     */
    public int deleteBuyerByPrimayKey(String buid){
        return buyerMapper.deleteByPrimaryKey(buid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public int deleteBuyerByExample(){
        return buyerMapper.deleteByExample(null);
    }



    public Buyer getByBuidWithOrderInfos(String buid){
        return buyerMapper.selectByBuidWithOrderInfos(buid);
    }

}
